package main

import (
	"github.com/cloudwego/hertz/pkg/app/server"
	"github.com/cloudwego/hertz/pkg/common/config"
	"github.com/cloudwego/hertz/pkg/common/tracer/stats"
	"github.com/ezcloud/ezapp/api/router"
	"github.com/ezcloud/ezapp/pkg/core/env"
	"github.com/ezcloud/ezapp/pkg/core/log"
	"github.com/ezcloud/ezapp/pkg/middleware/ctxcache"
	"github.com/ezcloud/ezapp/pkg/middleware/i18n"
	"github.com/ezcloud/ezapp/pkg/middleware/logs"
	"github.com/ezcloud/ezapp/pkg/middleware/security"
)

func main() {
	debug := env.GetEnv("APP_MODEL", "DEV")
	if debug == "DEV" {
		log.InitDevelopment()
	} else {
		log.InitProduction()
	}
	addr := env.GetEnv("LISTEN_ADDR", ":8888")

	opts := []config.Option{
		server.WithHostPorts(addr),
		server.WithTraceLevel(stats.LevelBase),
	}

	s := server.Default(opts...)

	// Middleware order matters
	s.Use(ctxcache.ContextCacheHandler()) // must be first
	s.Use(logs.GidHandler())
	s.Use(logs.AccessLogHandler())
	s.Use(i18n.I18nHandler())
	s.Use(security.SecurityHandler(router.EXCLUDE_URL))

	router.Register(s)

	s.Spin()
}
